/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package es.xpt.ysf.view;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.apache.myfaces.shared.util.MessageUtils;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.model.MessageWithParams;
import es.xpt.ysf.view.model.UserBean;
import es.xpt.ysf.web.Constants;

@SuppressWarnings("serial")
public abstract class Controller implements java.io.Serializable { // NO_UCD

	private UserBean userBean = null;
	private String messageBundle;

	public Controller() {
	}

	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	protected void setInfoMessage(List<MessageWithParams> messageIds) {
		if (messageIds != null) {
			for (MessageWithParams m : messageIds)
				setInfoMessage(m.getMessage(), m.getParams());
		}
	}

	
	protected void setInfoMessage(String messageId) {
		setInfoMessage(messageId, (Object[]) null);
	}

	protected void setInfoMessage(YSFException e) {
		setInfoMessage(e.getMessage(), e.getParameters());
	}

	protected void setInfoMessage(UIComponent component, String messageId) {
		setInfoMessage(component, messageId, (Object[]) null);
	}

	protected void setInfoMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_INFO, messageId, params);
	}

	protected void setInfoMessage(UIComponent component, String messageId,
			Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_INFO, messageId, params,
				component.getClientId(getFacesContext()));
	}

	protected void setWarnMessage(String messageId) {
		setWarnMessage(messageId, (Object[]) null);
	}
	
	protected void setWarnMessage(List<MessageWithParams> messageIds) {
		if (messageIds != null) {
			for (MessageWithParams m : messageIds)
				setWarnMessage(m.getMessage(), m.getParams());
		}
	}

	protected void setWarnMessage(YSFException e) {
		setWarnMessage(e.getMessage(), e.getParameters());
	}

	protected void setWarnMessage(UIComponent component, String messageId) {
		setWarnMessage(component, messageId, (Object[]) null);
	}

	protected void setWarnMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle ,FacesMessage.SEVERITY_WARN, messageId, params);
	}

	protected void setWarnMessage(UIComponent component, String messageId,
			Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_WARN, messageId, params,
				component.getClientId(getFacesContext()));
	}

	protected void setErrorMessage(String messageId) {
		setErrorMessage(messageId, (Object[]) null);
	}

	protected void setErrorMessage(List<MessageWithParams> messageIds) {
		if (messageIds != null) {
			for (MessageWithParams m : messageIds)
				setErrorMessage(m.getMessage(), m.getParams());
		}
	}
	
	protected void setErrorMessage(YSFException e) {
		setErrorMessage(e.getMessage(), e.getParameters());
	}

	protected void setErrorMessage(UIComponent component, String messageId) {
		setErrorMessage(component, messageId, (Object[]) null);
	}

	protected void setErrorMessage(String messageId, Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_ERROR, messageId, params);

	}

	protected void setErrorMessage(UIComponent component, String messageId,
			Object... params) {
		MessageUtils.addMessage(messageBundle, FacesMessage.SEVERITY_ERROR, messageId, params,
				component.getClientId(getFacesContext()));
	}

	protected Object getSessionAttribute(String key) {
		return getFacesContext().getExternalContext().getSessionMap().get(key);
	}
	

	/*
	 * protected ResourceBundle getMessagesBundle() { if ( messages!=null )
	 * return messages; FacesContext facesContext = getFacesContext(); messages =
	 * ((ApplicationViewBean) facesContext.getApplication()
	 * .getVariableResolver().resolveVariable(facesContext, "ApplicationBean"))
	 * .getMessagesBundle(facesContext.getViewRoot().getLocale()); return
	 * messages; }
	 */
	protected UserBean getUserBean() {
		if (userBean != null)
			return userBean;
//		FacesContext facesContext = getFacesContext();
//		userBean = (UserBean) facesContext.getApplication().getELResolver()
//				.getValue(facesContext.getELContext(), null,
//						Constants.USER_INFORMATION.getValue());
		userBean = (UserBean)getSessionAttribute(Constants.USER_INFORMATION.getValue());
		return userBean;
	}

	/**
	 * @return the messageBundle
	 */
	public String getMessageBundle() {
		return messageBundle;
	}

	/**
	 * @param messageBundle the messageBundle to set
	 */
	public void setMessageBundle(String messageBundle) {
		this.messageBundle = messageBundle;
	}

	protected Object setSessionAttribute(String key, Object value) {
		return getFacesContext().getExternalContext().getSessionMap().put(key,value);
	}

}
